package core;

import gui.ImportSymbol;
import gui.MainWindow;
import gui.SplashScreen;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.Highlight;
import javax.swing.text.Highlighter.HighlightPainter;

import res.Settings;

public class ThActionListener implements ActionListener, CaretListener{
  
  public static final String FILE_MENU_OPEN_ACTION = "menu_open";
  public static final String FILE_MENU_SAVE_ACTION = "menu_save";
  public static final String FILE_MENU_IMPORT_SYMBOL_ACTION = "menu_import_symbol";
  public static final String FILE_MENU_EXIT_ACTION = "menu_exit";
  
  public static final String HELP_MENU_ABOUT_ACTION = "menu_about";
  
  private MainWindow window;
  private boolean highlighterEnabled = false;

  public ThActionListener(MainWindow window)
  {
    this.window = window;
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals(FILE_MENU_OPEN_ACTION)) {
      JFileChooser chooser = new JFileChooser();
      File lastPath = Settings.getInstance().getFolder();
      if (lastPath != null)
        chooser.setSelectedFile(lastPath);
      int returnVal = chooser.showOpenDialog(this.window);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        Settings.getInstance().saveFolder(file);
        this.window.openFile(file);
      }
    }
    else if (e.getActionCommand().equals(FILE_MENU_SAVE_ACTION)) {
      JFileChooser chooser = new JFileChooser();

      File lastPath = Settings.getInstance().getFolder();
      if (lastPath != null)
        chooser.setSelectedFile(lastPath);
      int returnVal = chooser.showSaveDialog(this.window);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        Settings.getInstance().saveFolder(file);
        this.window.saveFile(file);
      }
    }
    else if (e.getActionCommand().equals(FILE_MENU_IMPORT_SYMBOL_ACTION)) {
      new ImportSymbol(window);
    }
    else if (e.getActionCommand().equals(FILE_MENU_EXIT_ACTION)) {
      System.exit(0);
    }
    else if (e.getActionCommand().equals(HELP_MENU_ABOUT_ACTION)) {
      SplashScreen screen = new SplashScreen(false);
      screen.setVisible(true);
    }
  }
  
  public void setHighlighterEnabled(boolean highlighterEnabled){
    this.highlighterEnabled = highlighterEnabled;
  }
    
  private class SelectionFoundPainter extends DefaultHighlighter.DefaultHighlightPainter{
    public SelectionFoundPainter(){
      super(new Color(250, 240, 205));
    }
  }

  public void caretUpdate(CaretEvent e) {
    if(e.getSource() instanceof JEditorPane && highlighterEnabled){
      try{
        JEditorPane pane = (JEditorPane)e.getSource();
        Document doc = pane.getDocument();
        HighlightPainter paint = new SelectionFoundPainter();
        Highlighter highlighter = pane.getHighlighter();
        
        Highlight[] highlights = highlighter.getHighlights();
        for(int i = 0; i < highlights.length; i++){
          if(highlights[i].getPainter() instanceof SelectionFoundPainter) {
            highlighter.removeHighlight(highlights[i]);
          }
        }
        
        String needle = pane.getSelectedText();
        String haystack = doc.getText(0, doc.getLength());
        
        if(needle != null && !needle.trim().equals("")){
          int pos = 0;
          while ((pos = haystack.indexOf(needle, pos)) >= 0){
            highlighter.addHighlight(pos, pos + needle.length(), paint);
            pos += needle.length();
          }
        }
      }
      catch(Exception ex){
        ex.printStackTrace();
      }
    }
  }
}